using System.Collections.Generic;
using UnityEngine;

public class MarketProductsPanel : MarketPanel
{
	public Product selectedProduct;

	public MarketProductButton selectedProductButton;

	public MarketProductButton equippedProductButton;

	public UICenterOnChild centerOnChild;

	public UIScrollView scrollView;

	public MarketMiniProductInfo productMiniInfoPanel;

	public MarketProductInfo productInfoPanel;

	public override void Awake()
	{
		base.Awake();
	}

	public void Start()
	{
		centerOnChild.onCenter = DisableCenterOnChild;
	}

	public override void InitButtons()
	{
		base.InitButtons();
		List<Product> listSortProducts = MarketController.Instance.GetListSortProducts(marketControllerUI.selectedCategory, marketControllerUI.selectedSubcategory);
		for (int i = 0; i < listSortProducts.Count; i++)
		{
			string text = string.Empty;
			if (i < 10)
			{
				text = "00";
			}
			else if (i < 100)
			{
				text = "0";
			}
			text += i;
			SetProductButton(listSortProducts[i], text);
		}
		if (selectedButton == null && panelButtons.Count > 0)
		{
			panelButtons[0].Select(true);
		}
		buttonsGrid.enabled = true;
		buttonsGrid.onReposition = CenterOnSelectedButton;
		foreach (MarketButton panelButton in panelButtons)
		{
			if (panelButton.selectedBG.gameObject.GetActive())
			{
				UIAnimation component = panelButton.GetComponent<UIAnimation>();
				if (!(component != null))
				{
				}
			}
		}
	}

	public void SetProductButton(Product product, string buttonName)
	{
		if ((((!(product is ProductCar) || product is ProductTitan) && (!(product is ProductWeapon) || product is ProductComsumable)) || product.Purchased || MarketController.Instance.IsMyTier(CupsManager.CurrentTier, product.tier)) && (bool)buttonExample)
		{
			MarketProductButton marketProductButton = (MarketProductButton)GetButtonFromPull();
			if (marketProductButton == null)
			{
				marketProductButton = (MarketProductButton)InstantiateButton();
			}
			marketProductButton.gameObject.SetActive(true);
			marketProductButton.InitProductButton(this, product);
			marketProductButton.name = buttonName;
			marketProductButton.GetComponent<UIPanel>().alpha = 0f;
			panelButtons.Add(marketProductButton);
		}
	}

	public override void ClearButtons()
	{
		for (int i = 0; i < panelButtons.Count; i++)
		{
			if (panelButtons[i] != equippedProductButton)
			{
				panelButtons[i].UnloadProductTexture();
			}
			panelButtons[i].gameObject.SetActive(false);
			buttonsPull.Add(panelButtons[i]);
		}
		panelButtons.Clear();
	}

	public override void Select(string item, bool selectDefault = true)
	{
		if (!string.IsNullOrEmpty(item) && !selected.Equals(item))
		{
			base.Select(item);
		}
	}

	public override void Unselect()
	{
		base.Unselect();
		productInfoPanel.HideInfoPanels();
	}

	public void ResetEquippedProductButton()
	{
		if ((bool)equippedProductButton)
		{
			equippedProductButton.ResetProductButtonInfo(false);
		}
		if ((bool)selectedProductButton)
		{
			selectedProductButton.ResetProductButtonInfo(true);
		}
		if (MarketControllerUI.instance.selectedCategory.Equals("Weapons") || MarketControllerUI.instance.selectedCategory.Equals("Armor"))
		{
			if (MarketController.Instance.GetEquipedProduct(MarketControllerUI.instance.selectedCategory, MarketControllerUI.instance.selectedSubcategory) != null)
			{
				marketControllerUI.subcategoriesPanel.selectedButton.icon.gameObject.SetActive(false);
				marketControllerUI.subcategoriesPanel.selectedButton.texture.gameObject.SetActive(true);
				if (!(selectedProductButton.product is ProductBoost))
				{
					marketControllerUI.subcategoriesPanel.selectedButton.LoadProductTexture(selectedProductButton.product);
				}
			}
			else
			{
				marketControllerUI.subcategoriesPanel.selectedButton.icon.gameObject.SetActive(true);
				marketControllerUI.subcategoriesPanel.selectedButton.texture.gameObject.SetActive(false);
			}
		}
		productInfoPanel.UpdateInfoPanel();
	}

	private void CenterOnSelectedButton()
	{
		centerOnChild.Recenter();
		scrollView.ResetPosition();
		if (selectedProductButton != null)
		{
			int num = panelButtons.IndexOf(selectedProductButton);
			if (num > buttonsGrid.maxPerLine)
			{
				centerOnChild.CenterOn(selectedProductButton.transform);
			}
		}
	}

	private void DisableCenterOnChild(GameObject go = null)
	{
		centerOnChild.enabled = false;
	}
}
